package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 07月 29日
 * Time: 下午2:47
 *
 * @QQ : 1234567890
 * **********************************
 */
public class EventVoteNumActivity extends MyBaseActivity implements View.OnClickListener{

    @FindViewById(id = R.id.weekly_date_lv)
    private ListView mListView;
    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;

    private ArrayList<String> mData = new ArrayList<String>();
    private EventVoteNumAdapter mAdapter;
    public int mSelectNumber;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.weekly_date_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);

    }

    @Override
    public void initData() {
        mTvTitle.setText("多选");
        mTvRight.setText("完成");
        mSelectNumber = getIntent().getIntExtra("number",2);
        for (int i = 0; i < 6 ; i++) {
            mData.add("最多选" + (i + 1) + "项");
        }
        mAdapter = new EventVoteNumAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mSelectNumber = i + 1;
                mAdapter.notifyDataSetChanged();
            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                Intent result = new Intent();
                result.putExtra("number",mSelectNumber);
                setResult(RESULT_OK, result);
                finish();
                break;
        }
    }

    class EventVoteNumAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            EventVoteNumActivity.ViewHolder holder;
            if (convertView == null){
                holder = new EventVoteNumActivity.ViewHolder();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.weekly_date_item_layout,null);
                holder.text = (TextView) convertView.findViewById(R.id.tv_weekly_date);
                holder.image = (ImageView) convertView.findViewById(R.id.iv_weekly_select);
                convertView.setTag(holder);
            }else {
                holder = (EventVoteNumActivity.ViewHolder) convertView.getTag();
            }
            holder.text.setText(mData.get(position));
            if (mSelectNumber == position + 1){
                holder.image.setVisibility(View.VISIBLE);
                holder.text.setTextColor(mActivity.getResources().getColor(R.color.weekly_main_orange_line));
            }else{
                holder.image.setVisibility(View.GONE);
                holder.text.setTextColor(mActivity.getResources().getColor(R.color.schedule_content));
            }
            return convertView;
        }
    }
    class ViewHolder{
        private TextView text;
        private ImageView image;
    }
}
